package com.thoughtservice.jspdoc;


import java.util.TreeSet;
import java.util.Vector;
import java.util.Iterator;
import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtservice.jspdoc.utils.FolderFilterUtils;

/**
 * Constructs a list of JSP Pages in a given directory.
 * 
 * @author alex
 */
public class JSPFileList {

	private final static Logger LOG = LoggerFactory.getLogger(JSPFileList.class);

	/**
	 * The list of jsp pages found under a given directory.
	 */
	private TreeSet<JSPPage> Pages;

	
	/**
	 * The root of the directory tree to look for JSPs under.
	 */
	private File RootDir;



	/**
	 * Construct the list of JSP pages under the named directory.
	 * 
	 * @param dir java.io.File The root directory to start looking from.
	 * @param indent String Used for displaying the hierarchy
	 */
	public JSPFileList(File dir, String indent) {
		
		Pages = new TreeSet<JSPPage>();
		RootDir = dir;
		
		processDir(dir, indent);


		// Add the Root directory
		Pages.add(new JSPPage(new File(dir + "/"), RootDir.toString()));

	}	
	
	


	/**
	 * <p>Walk a directory tree of JSP files looking for JSP and JSPF pages to document.
	 * This will create the list of pages in the <code>Pages</code> field.</p>
	 * 
	 * <p>This is a recursive method.</p>
	 * 
	 * <p>This will skip all <code>CVS</code>,<code>.svn</code> directories and all 
	 * <code>images</code>, <code>javascript</code>, <code>js</code>, <code>dojo</code>
	 * directories.</p>. This list is configurable. Check folderfilterlists.properties
	 * 
	 * @param dir java.io.File The root directory to start looking from.
	 * @param indent String Used for displaying the hierarchy
	 */
	private void processDir(File dir, String indent) {

		File[] files = dir.listFiles();
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				if (files[i].toString().indexOf("\\CVS") != -1);
				else if (files[i].toString().indexOf("\\images") != -1);
				else if (files[i].toString().indexOf("\\javascript") != -1);
				else if (files[i].toString().indexOf("\\js") != -1);
				else if (files[i].toString().indexOf("\\dojo") != -1);
				else if (files[i].toString().indexOf("\\.svn") != -1);
				//LOG.info("{} - {} ", files[i].toString().indexOf(FolderFilterUtils.getFilterList()), files[i]);
				//if(FolderFilterUtils.getFilterList().contains(files[i]));
				else {
					if (files[i].toString().indexOf("WEB-INF") == -1) {
//						System.out.println(indent + files[i]);
						JSPPage p = new JSPPage(files[i], RootDir.toString());
						Pages.add(p);
						processDir(files[i], indent + "   ");
					}
				}
			} else if (files[i].toString().endsWith(".jsp") || files[i].toString().endsWith(".jspf")) {
				JSPPage p = new JSPPage(files[i], RootDir.toString());
				Pages.add(p);
//				System.out.println(indent + files[i]);
			}
		}
	
	}



	/**
	 * Return the number of JSP pages found in the directory.
	 */
	public int listSize() {
		return Pages.size();
	}
	
	
	/**
	 * Returns an iterator over the JSP pages found in the directory.
	 */
	public Iterator<JSPPage> fileList() {
		return Pages.iterator();
	}

	/**
	 * Build the list of just the JSPPages that are in the list
	 */
	public Vector<JSPPage> fileListJSPs() {	
		Iterator<JSPPage> i = fileList();
		Vector<JSPPage> v = new Vector<JSPPage>();
		while (i.hasNext()) {
			JSPPage p = (JSPPage) i.next();
			if (p.getName().toLowerCase().endsWith(".jsp")
					|| p.getName().toLowerCase().endsWith(".jspf"))
				v.add(p);
		}
		return v;
	}



	/**
	 * Build the list of just the JSPPages that are in the list which
	 * also match some criteria in their ExtraData fields given by the
	 * <code>keyName</code> parameter with the value of the <code>value</code>
	 * parameter
	 */
	public Vector<JSPPage> fileListJSPs(String keyName, Object o) {	
		Iterator<JSPPage> i = fileList();
		Vector<JSPPage> v = new Vector<JSPPage>();
		while (i.hasNext()) {
			JSPPage p = (JSPPage) i.next();
			if ((p.getName().toLowerCase().endsWith(".jsp") || (p.getName()
					.toLowerCase().endsWith(".jspf"))) 
			 && p.getExtraData(keyName) != null
			 && p.getExtraData(keyName).equals(o)) {
				v.add(p);
			}
		}
		return v;
	}
	
	/**
         * Returns the root directory.
         *
         * @return java.io.File Root directory File object
         */
	public File getRootDir() {
	      return RootDir;
	}

	
}
